package com.bw.mvvm.mvvm.viewmodel;

import android.content.Context;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.bw.mvvm.entity.BaseEntity;
import com.bw.mvvm.mvvm.model.BaseModel;

import org.jetbrains.annotations.NotNull;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.HashMap;

import io.reactivex.Observer;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

public abstract class BaseViewModel<M extends BaseModel>extends ViewModel implements IViewModel, Observer<BaseEntity> {

    protected M m;

    protected CompositeDisposable compositeDisposable;

    private UIChange uiChange;


    @Override
    public void onCreate() {
        compositeDisposable = new CompositeDisposable();
        initModel();
    }
    private void initModel(){
        //反射获取泛型class类型 -- 创建对象
        ParameterizedType genericSuperclass = (ParameterizedType) getClass().getGenericSuperclass();
        Type[]types = genericSuperclass.getActualTypeArguments();
        Class cs = (Class) types[0];
        try {
            m = (M) cs.newInstance();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
    }

    protected abstract void result(BaseEntity entity);

    @Override
    public void onDestroy() {
        compositeDisposable.dispose();
        compositeDisposable.clear();
    }

    @Override
    public void onSubscribe(@NotNull Disposable d) {
        compositeDisposable.add(d);
    }

    @Override
    public void onNext(@NotNull BaseEntity baseEntity) {
        result(baseEntity);
    }

    @Override
    public void onError(@NotNull Throwable e) {
        showMsg("请求出错"+e.getMessage());
    }

    @Override
    public void onComplete() {
        hideDialog();
    }

    //提供到View层进行LiveData数据绑定
    //数据驱动View层对应观察者
    //观察者下调用对应封装的显示对话框等方法
    public UIChange getUiChange(){
        if (uiChange == null){
            uiChange = new UIChange();
        }
        return uiChange;
    }

    protected void showDialog(){
        getUiChange().setShowDialogObservable(""+System.currentTimeMillis());
    }
    protected void hideDialog(){
        getUiChange().setHideDialogObservable(""+System.currentTimeMillis());
    }
    protected void showMsg(String msg){
        getUiChange().setShowMsgObservable(""+System.currentTimeMillis());
    }
    protected void finishPage(){
        getUiChange().setFinishActObservable(""+System.currentTimeMillis());
    }
    protected void startPage(HashMap<String,Object> map){
        getUiChange().setStartPageObservable(map);
    }

    public class UIChange{
        private MutableLiveData<String> showDialogObservable = new MutableLiveData<>();
        private MutableLiveData<String> hideDialogObservable = new MutableLiveData<>();
        private MutableLiveData<String> showMsgObservable = new MutableLiveData<>();
        private MutableLiveData<String> finishActObservable = new MutableLiveData<>();
        private MutableLiveData<HashMap<String,Object>> startPageObservable = new MutableLiveData<>();

        //将获取到的值 赋值到当前ui界面
        public void setShowDialogObservable(String showDialogObservable){
            this.showDialogObservable.setValue(showDialogObservable);
        }
        public void setHideDialogObservable(String hideDialogObservable){
            this.hideDialogObservable.setValue(hideDialogObservable);
        }
        public void setShowMsgObservable(String showMsgObservable){
            this.showMsgObservable.setValue(showMsgObservable);
        }
        public void setFinishActObservable(String finishActObservable){
            this.finishActObservable.setValue(finishActObservable);
        }
        public void setStartPageObservable(HashMap<String,Object> startPageObservable){
            this.startPageObservable.setValue(startPageObservable);
        }

        public MutableLiveData<String> getShowDialogObservable(){
            return showDialogObservable;
        }

        public MutableLiveData<String> getHideDialogObservable(){
            return hideDialogObservable;
        }
        public MutableLiveData<String> getShowMsgObservable(){
            return showMsgObservable;
        }
        public MutableLiveData<String> getFinishActObservable(){
            return finishActObservable;
        }
        public MutableLiveData<HashMap<String,Object>> getStartPageObservable(){
            return startPageObservable;
        }
    }

}
